package cn.tj.wenda.cloud.other.service.impl;

import cn.tj.wenda.cloud.other.entity.Books;
import cn.tj.wenda.cloud.other.mapper.BooksMapper;
import cn.tj.wenda.cloud.other.service.BookService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Transactional
@Component
public class BookServiceImpl implements BookService {

    @Autowired
    BooksMapper booksMapper;

    /**
     * 取得所有图书一览
     *
     * @return 所有图书
     */
    @Override
    public PageInfo<Books> getBookList(int page, int limit, String params) {
        Example ex;
        if(params==null || params.equals(""))
            ex = null;
        else {
            ex = new Example(Books.class);
            ex.or().andLike("bookname","%"+ params + "%");
        }
        ex = new Example(Books.class);
        ex.or().andLike("bookname","%string%");
        PageHelper.startPage(page, limit, true);
        List<Books> result = booksMapper.selectByExample(ex);
        return new PageInfo<Books>(result);
    }

    /**
     * 更新一条图书数据
     *
     * @param book
     */
    @Override
    public int updateBook(String bookId, Books book) throws Exception {
        book.setBookid(bookId);
        return booksMapper.updateByPrimaryKeySelective(book);
    }

    /**
     * 添加一条图书数据
     *
     * @param book
     */
    @Override
    public int insertBook(Books book) {
        return booksMapper.insertSelective(book);
    }

    /**
     * 删除一条图书数据
     *
     * @param bookId
     */
    @Override
    public int deleteBook(String bookId) {
        return booksMapper.deleteByPrimaryKey(bookId);
    }
}
